# coding=utf-8
import os
from git.repo import Repo
import datetime
import os
from sshclient import SSHProxy
from modes import Hosts
from utils import Codezip


def git(prinfo, taskinfo):
    date = datetime.datetime.now().strftime('%Y%m%d%H')
    path = '/Users/dayang/PycharmProjects/Devops/codes/' + date + '/' + prinfo.project + '/' + taskinfo.tag
    if os.path.exists(path):
        Repo.clone_from(url=prinfo.repo, to_path=path, branch=taskinfo.tag)  # 下载代码
        Codezip(path, path)
        Filepath = path + '/' + \
                   prinfo.project + '/' + taskinfo.tag + '.zip '  # 获取下载文件路径
        print("获取文件路径:%s" % Filepath)
    else:
        os.makedirs(path)  # 创建下载目录
        Repo.clone_from(url=prinfo.repo, to_path=path, branch=taskinfo.tag)
        Filepath = path + '/' + \
                   prinfo.project + '/' + taskinfo.tag + '.zip '  # 获以下载文件路径
        print("lin25 获取文件路径:%s" % Filepath)
    # print(filepath)
    # Exists = os.path.exists(path)
    # print(" lin 19 exits status %s" % Exists)
    # if os.path.exists(path):
    #     print("21:path：%s" % path)
    #     print("文件夹存在删除掉")
    #     serverlist = prinfo.servers.split(',')
    #     # for host in serverlist:
    #     #     hostinfo = Hosts.query.filter_by(hostname=host).first()
    #     #     with SSHProxy(hostname=host, port=hostinfo.port, username=hostinfo.username,
    #     #                   password=hostinfo.passwd) as ssh:
    #     # ssh.upload(filepath, prinfo.path)
    #     # os.remove(path)
    # else:
    #     print("文件夹不存在")
    #     print("32:path：%s" % path)
    #     os.makedirs(path)
    #     Repo.clone_from(url=prinfo.repo, to_path=path, branch=taskinfo.tag)
    #     codezip(path, path)
    #     # print(prinfo.servers)
    #     serverlists = prinfo.servers.split('', )
    #     # print(type(serverlists))
    #     # for server in serverlists:
    #     #     print(server)
    #     #     print(type(server))
